package team526.config;

import com.github.pagehelper.PageHelper;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @author 王铭 20181620310021
 * #Date 2021/9/16 - 17:55
 */
@org.springframework.context.annotation.Configuration
public class Configuration implements WebMvcConfigurer {
    @Bean
    public CommonsMultipartResolver multipartResolver() {

        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(420000000);
        return multipartResolver;
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        rabbitTemplate.setConnectionFactory(connectionFactory);
        return rabbitTemplate;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserInterceptor())
                .addPathPatterns("/user/**", "user/*")
                .order(2)
                .excludePathPatterns("/user/login", "/user/login_f")
                .excludePathPatterns("/user/register", "/user/register_f")
                .excludePathPatterns("/user/check_email_if_used", "/user/check_name_if_used")
                .excludePathPatterns("/user/getVideoRepository")
                .excludePathPatterns("/user/info/*");
//        registry.addInterceptor(new OriginInterceptor())
//                .addPathPatterns("/**").order(1);
        registry.addInterceptor(new AdminInterceptor())
                .addPathPatterns("/admin/*").addPathPatterns("/admin/**").excludePathPatterns("/admin/login");
    }

//    @Bean
    public PageHelper pageHelper() {
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        properties.setProperty("reasonable", "true");
        properties.setProperty("dialect", "mysql");    //配置mysql数据库的方言
        pageHelper.setProperties(properties);
        return pageHelper;
    }


}
